local M = {}

local path={}
for k,v in pairs(workspace.CameraPath:GetChildren()) do
	path[tonumber(v.Name)]=v
end
local pathCount=#path
local TweenService= game:GetService("TweenService")
local TW=nil
local function move (index)
	
	if index>pathCount then
		index=1
	end
	
	local s=path[index]
	local e=nil
	
	if index+1>pathCount then
		e=path[1]
	else
		e=path[index+1]
	end
	
	workspace.Camera.CFrame=s.CFrame
	local tweenInfo = TweenInfo.new(s:GetAttribute("time"), Enum.EasingStyle.Linear)
	TW = TweenService:Create(workspace.Camera,tweenInfo, {CFrame=e.CFrame})
	TW.Completed:Connect(function(playbackState)
		TW:Destroy()
		TW=nil
		move(index+1)
	end)
	TW:Play()
end



M.play=function()
	workspace.Camera.CameraType = Enum.CameraType.Scriptable

	move(1)
end

M.stop=function()
	if TW then
		TW:Cancel ( )
		TW:Destroy()
		TW=nil
	end
	
	workspace.Camera.CameraType = Enum.CameraType.Custom

end


M.stopTW=function()
	if TW then
		TW:Cancel ( )
		TW:Destroy()
		TW=nil
	end

end

return M
